package com.etlmaster.executor;

import java.util.List;

import com.etlmaster.executor.Job;
import com.etlmaster.executor.StepMeta;
import com.etlmaster.executor.steps.control.CheckTableStepInterface;
import com.etlmaster.executor.steps.control.ConditionalStepInterface;
import com.etlmaster.executor.steps.control.ControlStepMeta;
import com.etlmaster.executor.steps.control.RingStepMeta;
import com.etlmaster.executor.utils.LogWriter;

public class DependencyCheck {
	/**
	 * 前置依赖都准备好
	 * @return
	 */
	
	public static boolean isPreDependentReady(String subsession_id,StepMeta step,Job job){
		
		if(!(step instanceof RingStepMeta)){//忽略环状节点
			List<StepMeta> preStepMetas = job.getPreStepMetas(step.getId());
			
			for (int i = 0; preStepMetas !=null && i < preStepMetas.size(); i++) {
				StepMeta stepMeta = preStepMetas.get(i);
				StepMeta step1= (StepMeta) job.getRunOffStepMetaById(subsession_id,stepMeta.getId());
				if(step1 != null){
					if(stepMeta instanceof ConditionalStepInterface){//如果前序节点是条件节点
						return step1.getStepState().isDone();
					}else if(!step1.getStepState().isDone()){
						return false;
					}
				}else if(step1 == null){
					if(isPreDependentPath(subsession_id,stepMeta,job))//有路径可达，等待期路径的节点执行完成，等待下次触发运行
						return false;
					else //路径上任务可以不执行，则返回条件满足，不管其分支依赖直接运行
						return true;
				}
			}
		}
		return true;
	}

	public static boolean isPreDependentPath(String subsession_id,StepMeta stepMeta,Job job){
		List<StepMeta> preStepMetas = job.getPreStepMetas(stepMeta.getId());
		if(preStepMetas.size()==0){//前置依赖为空，证明运行路径是可达的
			return true;
		}else{
			for (int i = 0; i < preStepMetas.size(); i++) {
				StepMeta prsStepMeta = preStepMetas.get(i);
				StepMeta step= (StepMeta) job.getRunOffStepMetaById(subsession_id,stepMeta.getId());
				if(step != null){//已经运行
					if(checkIsRanStep( prsStepMeta, job)){//如果前序节点是条件节点,证明不可达
						return false;
					}
				}else if(step == null){//未运行
					return isPreDependentPath(subsession_id,prsStepMeta,job);
				}
			}
		  return true;
		}
	}
	private static void runningByStep(StepMeta stepMeta,Job job){
		if(job.getVariable("jobStartRunStepId")!=null){//步骤号不为空
			String jobStartRunStepId=job.getVariable("jobStartRunStepId").toString();
			String stepId=stepMeta.getId();
			if(stepId.equals(jobStartRunStepId)){//当步骤号与断点一直时将全局变量值为flase，后续就自动执行了
				LogWriter.addLog("INFO","跳转到执行步骤了。");
				job.getVariables().put("skipStep","false");
			}
			if(Boolean.parseBoolean(job.getVariable("skipStep").toString())==true){//如果忽略的步骤号标志位
				stepMeta.setSkipFlag(Boolean.parseBoolean(job.getVariable("skipStep").toString()));
				if(stepMeta instanceof ControlStepMeta){//特殊节点，如是变量节点，就放开运行
					stepMeta.setSkipFlag(false);
				}
			}

		}
	}
	private static void endByStep(StepMeta stepMeta,Job job){
		if(stepMeta.getId().equals(job.getVariable("jobStopStepId")))
		{
			LogWriter.addLog("INFO","到达停止步骤，步骤id:{}",stepMeta.getId());
			EtlMasterExecutor.stop(job);
		}
	}
	public static void debugMode(StepMeta stepMeta,Job job){
		runningByStep(stepMeta,job);
		endByStep(stepMeta,job);
	}
	public static boolean checkIsRanStep(StepMeta prsStepMeta,Job job){
		String stepId=prsStepMeta.getId();
		String successStepId=job.getSucessStepId(stepId);
		String failedStepId=job.getFailStepId(stepId);
		if(prsStepMeta instanceof ConditionalStepInterface||prsStepMeta instanceof CheckTableStepInterface){//如果前序节点是条件节点,证明不可达
			return true;
		}else if(!(successStepId.equals("-1")||successStepId.equals("99"))&&!(failedStepId.equals("-1")||failedStepId.equals("99"))){
			return true;
		}else{
			return false;
		}
	}

}
